package com.leyou.item.controller;

import com.leyou.common.page.pageResult;
import com.leyou.common.utils.BeanHelper;
import com.leyou.item.dto.SkuDTO;
import com.leyou.item.dto.SpuDTO;
import com.leyou.item.dto.SpuDetailDTO;
import com.leyou.item.entity.TbSpu;
import com.leyou.item.service.GoodsService;
import com.leyou.item.service.TbSpuService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
public class GoodsController {

    @Autowired
    private GoodsService goodsService;

    @Autowired
    private TbSpuService spuService;

    @GetMapping("/spu/page")
    public ResponseEntity<pageResult<SpuDTO>> findSpuByPage(@RequestParam(value = "page",required = false,defaultValue = "1")Integer page,
                                                            @RequestParam(value = "rows",required = false,defaultValue = "5")Integer rows,
                                                            @RequestParam(value = "key",required = false)String key,
                                                            @RequestParam(value = "saleable",required = false)Boolean saleable){

        pageResult<SpuDTO> pageResult=goodsService.findSpuByPage(page,rows,key,saleable);

        return ResponseEntity.status(HttpStatus.OK).body(pageResult);
    }


    @PostMapping("/goods")
    public ResponseEntity<Void> saveGoods(@RequestBody SpuDTO spuDTO){

        goodsService.saveGoods(spuDTO);

        return ResponseEntity.status(HttpStatus.CREATED).build();

    }

    @PutMapping("spu/saleable")
    public ResponseEntity<Void> saleableGoods(@RequestParam(value = "id",required = true)Long id,
                                              @RequestParam(value = "saleable",required = true)Boolean saleable){

        goodsService.saleableGoods(id,saleable);

        return ResponseEntity.status(HttpStatus.OK).build();
    }

    @GetMapping("spu/detail")
    public ResponseEntity<SpuDetailDTO> findSpuDetailBySpuId(@RequestParam("id")Long SpuId){

        SpuDetailDTO    spuDetailDTO=goodsService.findSpuDetailBySpuId(SpuId);

        return ResponseEntity.status(HttpStatus.OK).body(spuDetailDTO);

    }

    /*根据id查询sku数据：返回值sku数据集合*/
    @GetMapping("sku/of/spu")
    public ResponseEntity<List<SkuDTO>> findSkuBySpuId(@RequestParam("id")Long SpuId){

        List<SkuDTO>  skuDTOList=goodsService.findSkuBySpuId(SpuId);

        return ResponseEntity.status(HttpStatus.OK).body(skuDTOList);
    }


    /*更新商品*/
    @PutMapping("/goods")
    public ResponseEntity<Void> updateGoods(@RequestBody SpuDTO spuDTO){

        goodsService.updateGoods(spuDTO);

        return ResponseEntity.status(HttpStatus.CREATED).build();

    }
    @GetMapping("spu/{id}")
    public ResponseEntity<SpuDTO> findSpuById(@PathVariable("id") Long ids){

        TbSpu tbSpu = spuService.getById(ids);
        /*对象转化*/
        SpuDTO spuDTO = BeanHelper.copyProperties(tbSpu, SpuDTO.class);

        return ResponseEntity.status(HttpStatus.OK).body(spuDTO);
    }
}
